﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MapLibrary.Utility;
using Microsoft.Xna.Framework.Content;
using Animation;
using Microsoft.Xna.Framework;
using MapLibrary.Players;
using FarseerPhysics;
using MapLibrary.Players.Skills;
using MapLibrary.Players.Abilities.MapLibrary.Players.Skills;

namespace MapLibrary.MapLibrary
{
    public class ProjectileManager
    {
        private SpritePool<Bullet> bulletPool;
        private SpritePool<LaserInstance> laserPool;
        private SpritePool<NetSprite> netPool;
        private SpritePool<NetSprite> largeNetPool;
        private SpritePool<Hook> hookPool;
        private GameWorld world;
        private Vector2 lastMousePosition;

        public ProjectileManager(GameWorld world)
        {
            this.world = world;
        }

        public void Load(ContentManager manager)
        {
            SpriteType bulletType = manager.Load<SpriteType>(@"Animation/SpriteTypes/Bullet");
            SpriteType netType = manager.Load<SpriteType>(@"Animation/SpriteTypes/Net");
            SpriteType largeNetType = manager.Load<SpriteType>(@"Animation/SpriteTypes/LargeNet");
            SpriteType hookType = manager.Load<SpriteType>(@"Animation/SpriteTypes/Hook");
            SpriteType laserType = manager.Load<SpriteType>(@"Animation/SpriteTypes/Laser");

            bulletPool = new SpritePool<Bullet>(world, bulletType);
            netPool = new SpritePool<NetSprite>(world, netType);
            largeNetPool = new SpritePool<NetSprite>(world, largeNetType);
            hookPool = new SpritePool<Hook>(world, hookType);
            laserPool = new SpritePool<LaserInstance>(world, laserType);
        }

        public void UpdateMousePosition(Vector2 newPos)
        {
            lastMousePosition = newPos;
        }

        public void AddBullet(Player p)
        {
            Bullet b = bulletPool.New();

            b.Body.IgnoreGravity = true;
            b.Body.IsBullet = true;

            b.Position = p.Position;
            //b.Position = b.Position + b.GetCenter();
            Vector2 viewportBulletPos = world.Viewport.WorldToViewportCoords(b.Position);
            Vector2 direction = -(viewportBulletPos - lastMousePosition);
            Vector2 dFinal = Vector2.Normalize(direction);
            dFinal *= 10.0f;

            b.Body.LinearVelocity = dFinal;

            world.CurrentLevel.AddSprite(b);
        }

        public Sprite AddNet(Player p, float velocity = 10.0f)
        {
            NetSprite b = netPool.New();

           // b.Body.IgnoreGravity = true;
            b.Body.IsBullet = true;

            b.Position = p.Position;
            //b.Position = b.Position + b.GetCenter();
            Vector2 viewportBulletPos = world.Viewport.WorldToViewportCoords(b.Position);
            Vector2 direction = -(viewportBulletPos - lastMousePosition);
            Vector2 dFinal = Vector2.Normalize(direction);
            dFinal *= velocity;

            b.Body.LinearVelocity = dFinal;

            float angle = (float)Math.Atan2(direction.Y, direction.X);
            //b.Body.Rotation = angle;

            //b.Body.SetTransform(b.Body.Position, angle);

            world.CurrentLevel.AddSprite(b);

            return b;
        }

        public Sprite AddLargeNet(Player p, float velocity = 10.0f)
        {
            NetSprite b = largeNetPool.New();

            // b.Body.IgnoreGravity = true;
            b.Body.IsBullet = true;

            b.Position = p.Position;
            //b.Position = b.Position + b.GetCenter();
            Vector2 viewportBulletPos = world.Viewport.WorldToViewportCoords(b.Position);
            Vector2 direction = -(viewportBulletPos - lastMousePosition);
            Vector2 dFinal = Vector2.Normalize(direction);
            dFinal *= velocity;

            b.Body.LinearVelocity = dFinal;

            float angle = (float)Math.Atan2(direction.Y, direction.X);
            //b.Body.Rotation = angle;

            //b.Body.SetTransform(b.Body.Position, angle);

            world.CurrentLevel.AddSprite(b);

            return b;
        }

        public Sprite AddHook(Player p, float velocity = 10.0f)
        {
            Hook b = hookPool.New();

            b.Body.IgnoreGravity = true;
            b.Body.IsBullet = true;

            b.Position = p.Position;
            //b.Position = b.Position + b.GetCenter();
            Vector2 viewportBulletPos = world.Viewport.WorldToViewportCoords(b.Position);
            Vector2 direction = -(viewportBulletPos - lastMousePosition);
            Vector2 dFinal = Vector2.Normalize(direction);
            dFinal *= velocity;

            b.Body.LinearVelocity = dFinal;

            float angle = (float)Math.Atan2(direction.Y, direction.X);

            b.Body.Rotation = angle;

            world.CurrentLevel.AddSprite(b);

            return b;
        }

        public Sprite AddLaserInstance(Player p, float velocity = 5.0f)
        {
            LaserInstance l = laserPool.New();
            l.Body.IgnoreGravity = true;
            l.Position = p.Position ;//+ new Vector2(50,0); L = P+LASERCHUNK LENGTH!

            //Vector2 viewportLaserPos = world.Viewport.WorldToViewportCoords(l.Position);
            //Vector2 direction = -(viewportLaserPos - lastMousePosition);
            //Vector2 dFinal = Vector2.Normalize(direction);
            //dFinal *= velocity;
            l.Body.LinearVelocity = Vector2.Zero;
            world.CurrentLevel.AddSprite(l);
            return l;
        }

        public void RemoveBullet(Bullet b)
        {
            bulletPool.Reclaim(b);
        }

    }
}
